home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-02-05 | 47.9 KB | 1,794 lines | [TEXT/MPS ] |
- // Copyright © 1991 Apple Computer, Inc. All rights reserved.
-
- #ifndef __UCORE__
- #include "UCore.h"
- #endif
-
- extern TCoreApplication * gTaroApplication;
-
- // ============================== AppleEvent Handler=================================
- pascal OSErr ReceiveBeginTransaction(AppleEvent & theAppleEvent, AppleEvent & reply,
- long /*handlerRefcon*/)
- {
- TBeginTransactionCommand * aBeginTransactionCommand = new TBeginTransactionCommand;
- FailNIL(aBeginTransactionCommand);
- aBeginTransactionCommand->IBeginTransactionCommand(theAppleEvent, reply);
- gTaroApplication->PostCommand(aBeginTransactionCommand);
-
- return noErr;
- }
-
- // ------------------------------------------------------------------------
- pascal OSErr ReceiveEndTransaction(AppleEvent & theAppleEvent, AppleEvent & reply,
- long /*handlerRefcon*/)
- {
- DescType typeCode;
- Size actualSize;
- TAppleEventPlayer * aPlayer;
- FailInfo fi;
- long longError = noErr;
-
- if(fi.Try())
- {
- FailOSErr(AEGetAttributePtr(theAppleEvent, keyTransactionIDAttr, typeLongInteger,
- typeCode, (Ptr)&aPlayer, sizeof(aPlayer), actualSize));
- if(IsObject(aPlayer))
- {
- if(IsMemberClassID(aPlayer, GetClassIDFromName("TAppleEventPlayer")))
- {
- aPlayer->EndTransaction();
- }
- else
- longError = errAEEventNotHandled;
- }
- else
- longError = errAEEventNotHandled;
- if(longError != noErr)
- FailOSErr(AEPutParamPtr(reply, keyErrorNumber, typeLongInteger,
- (Ptr)&longError, sizeof(longError)));
- fi.Success();
- }
- else
- {
- longError = errAEFail;
- FailOSErr(AEPutParamPtr(reply, keyErrorNumber, typeLongInteger,
- (Ptr)&longError, sizeof(longError)));
- fi.ReSignal();
- }
- return noErr;
- }
-
- // ------------------------------------------------------------------------
- pascal OSErr ReceiveNewElement(AppleEvent & theAppleEvent, AppleEvent & reply,
- long /*handlerRefcon*/)
- {
- TNewElementCommand * aNewElementCommand = new TNewElementCommand;
- FailNIL(aNewElementCommand);
- aNewElementCommand->INewElementCommand(theAppleEvent, reply);
- gTaroApplication->InsertNewElementCommand(aNewElementCommand);
-
- return noErr;
- }
-
- // ------------------------------------------------------------------------
- pascal OSErr ReceiveSetData(AppleEvent & theAppleEvent, AppleEvent & reply,
- void * /*refcon*/)
- {
- TSetDataCommand * aSetDataCommand = new TSetDataCommand;
- aSetDataCommand->ISetDataCommand(theAppleEvent, reply);
- gTaroApplication->PostCommand(aSetDataCommand);
- return noErr;
- }
-
- // ------------------------------------------------------------------------
- pascal OSErr ReceiveAbout(AppleEvent & /*theAppleEvent*/, AppleEvent & /*reply*/,
- void * /*refcon*/)
- {
- TAboutBoxCommand * anAboutBoxCommand = new TAboutBoxCommand;
- anAboutBoxCommand->IAboutBoxCommand(cAboutApp);
- gTaroApplication->PostCommand(anAboutBoxCommand);
- return noErr;
- }
-
- // ------------------------------------------------------------------------
- pascal OSErr ReceiveUnknown(AppleEvent & /*theAppleEvent*/, AppleEvent & /*reply*/,
- long /*handlerRefcon*/)
- {
- return noErr;
- }
-
- // ============================== Service Routines =================================
- char TranslateCellSize(char anItem)
- {
- switch(anItem)
- {
- case 1:
- return 4;
- case 2:
- return 6;
- case 3:
- default:
- return 8;
- case 4:
- return 12;
- case 5:
- return 16;
- case 6:
- return 24;
- case 7:
- return 32;
- }
- }
-
- // ------------------------------------------------------------------------
- char RetranslateCellSize(char CellSize)
- {
- switch(CellSize)
- {
- case 4:
- return 1;
- case 6:
- return 2;
- case 8:
- default:
- return 3;
- case 12:
- return 4;
- case 16:
- return 5;
- case 24:
- return 6;
- case 32:
- return 7;
- }
- }
-
- // ------------------------------------------------------------------------
- #pragma segment Main
- void ShowModalDialog(short DialogID)
- {
- short itemHit;
-
- DialogPtr aDialog = GetNewDialog(DialogID, NULL, (GrafPtr)-1);
- ModalDialog(NULL, itemHit);
- DisposDialog(aDialog);
- }
-
- // ------------------------------------------------------------------------
- #pragma segment Main
- OSErr CheckPPC(void)
- {
- const short kPPCToolboxDisabled = 1002;
- const short kAppleTalkDisabled = 1000;
- const short kProgramLinkingDisabled = 1001;
- const short kAppleEventNotAvailable = 1003;
- long PPCAttributes, AppleEventAttributes;
- OSErr Error = noErr;
-
- if(Gestalt(gestaltAppleEventsAttr, AppleEventAttributes) != noErr)
- {
- return kAppleEventNotAvailable;
- }
-
- if(Gestalt(gestaltPPCToolboxAttr, PPCAttributes) == noErr)
- {
- if((PPCAttributes & gestaltPPCSupportsOutGoing) == 0)
- {
- Error = kAppleTalkDisabled;
- }
- else if((PPCAttributes & gestaltPPCSupportsIncoming) == 0)
- {
- Error = kProgramLinkingDisabled;
- }
- }
- else
- {
- Error = kPPCToolboxDisabled;
- }
- return Error;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AClose
- void InstallNotification(void)
- {
- NMRec Notification;
-
- Notification.qType = 8;
- Notification.nmMark = 1;
- Notification.nmIcon = GetResource('ics#', 128);
- Notification.nmSound = GetResource('snd ', 1000);
- Notification.nmStr = NULL;
- Notification.nmResp = (NMProcPtr)-1;
- Notification.nmRefCon = NULL;
-
- NMInstall(&Notification);
- }
-
-
- // ------------------------------------------------------------------------
- #pragma segment ARes
- pascal OSErr TaroTableAccessor( DescType desiredClass,
- const AEDesc &container, DescType /*containerClass*/, DescType form,
- const AEDesc &selectionData, AEDesc &value, long /*LongInt*/ )
- {
- OSErr anError = errAEEventNotHandled;
- if((desiredClass == cTable) && (container.descriptorType == typeNull))
- {
- if((selectionData.descriptorType == typeChar) && (form == formName))
- {
- Str255 documentName;
-
- GetIText(selectionData.dataHandle, documentName);
- TDocument * aDocument = gTaroApplication->FindDocumentFromName(documentName);
- if(IsObject(aDocument))
- {
- FailOSErr(AECreateDesc(typeTObjectPtr, (Ptr)&aDocument, sizeof(aDocument), value));
- anError = noErr;
- }
- }
- }
- if(anError != noErr)
- FailOSErr(AECreateDesc(typeNull, NULL, 0, value));
-
- return anError;
- }
-
-
- // ------------------------------------------------------------------------
- #pragma segment ARes
- pascal OSErr TaroCellAccessor( DescType desiredClass,
- const AEDesc &container, DescType containerClass, DescType form,
- const AEDesc &selectionData, AEDesc &value, long /*LongInt*/ )
- {
- OSErr anError = errAEEventNotHandled;
- if((desiredClass == cCell) && (containerClass == cTable))
- {
- TCoreDocument * aCoreDocument =(TCoreDocument *)(**((TObjectPtrHandle)container.dataHandle));
- if(IsObject(aCoreDocument))
- {
- if(IsMemberClassID(aCoreDocument, GetClassIDFromName("TCoreDocument")))
- {
- if((selectionData.descriptorType == typeLongInteger) && (form == formAbsolutePosition))
- {
- short aCellIndex = (short)(**((LongIntHandle)selectionData.dataHandle) - 1);
- char aRowSize, aColumnSize;
- aCoreDocument->GetCellSize(aColumnSize, aRowSize);
- TRecordCell * aMove = new TRecordCell;
- aMove->IRecordCell(aCellIndex % aColumnSize, aCellIndex / aColumnSize, kEmpty);
-
- FailOSErr(AECreateDesc(typeTObjectPtr, (Ptr)&aMove, sizeof(aMove), value));
- return noErr;
- }
- }
- }
- }
- if(anError != noErr)
- FailOSErr(AECreateDesc(typeNull, NULL, 0, value));
-
- return anError;
- }
-
-
- // ============================== TCoreApplication =================================
- #pragma segment AInit
- pascal void TCoreApplication::ITaroApplication(void)
- {
- IApplication(kFileType, kSignature);
-
- FailOSErr(AEInstallEventHandler(typeWildCard, typeWildCard,
- (EventHandlerProcPtr)ReceiveUnknown, 0, FALSE));
-
- FailOSErr(AEInstallEventHandler(kAECoreSuite, kAEBeginTransaction,
- (EventHandlerProcPtr)ReceiveBeginTransaction, 0, FALSE));
- FailOSErr(AEInstallEventHandler(kAECoreSuite, kAEEndTransaction,
- (EventHandlerProcPtr)ReceiveEndTransaction, 0, FALSE));
- FailOSErr(AEInstallEventHandler(kAECoreSuite, kAENewElement,
- (EventHandlerProcPtr)ReceiveNewElement, 0, FALSE));
- FailOSErr(AEInstallEventHandler(kAECoreSuite, kAESetData,
- (EventHandlerProcPtr)ReceiveSetData, 0, FALSE));
- FailOSErr(AEInstallEventHandler(kAECoreSuite, kAEAbout,
- (EventHandlerProcPtr)ReceiveAbout, 0, FALSE));
-
- FailOSErr(AEInstallObjectAccessor(cTable, typeWildCard,
- TaroTableAccessor, 0, FALSE));
- FailOSErr(AEInstallObjectAccessor(cCell, typeWildCard,
- TaroCellAccessor, 0, FALSE));
-
- fLaunchWithNewDocument = FALSE;
-
- TList * aList = NewList();
- FailNIL(aList);
- fNewElementCommandList = aList;
- fNewElementCommandList->SetEltType("TNewElementCommand");
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AOpen
- pascal void TCoreApplication::InsertNewElementCommand(TNewElementCommand * aNewElementCommand)
- {
- if(fNewElementCommandList->GetSize() == 0)
- PostCommand(aNewElementCommand);
- fNewElementCommandList->InsertLast(aNewElementCommand);
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AOpen
- pascal void TCoreApplication::RemoveNewElementCommand(void)
- {
- fNewElementCommandList->AtDelete(1);
- if(fNewElementCommandList->GetSize() != 0)
- PostCommand((TCommand *)fNewElementCommandList->At(1));
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AOpen
- pascal TNewElementCommand * TCoreApplication::GetNewElementCommand(void)
- {
- if(fNewElementCommandList->GetSize() != 0)
- return (TNewElementCommand *)fNewElementCommandList->At(1);
- else
- return NULL;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AOpen
- pascal TDocument * TCoreApplication::FindDocumentFromName(Str255 & theName)
- {
- CDocumentIterator iter(this);
-
- for(TDocument * aDocument = iter.FirstDocument(); iter.More(); aDocument = iter.NextDocument())
- {
- Str255 aName;
-
- aDocument->GetTitle(aName);
- if(aName == theName)
- return aDocument;
- }
- return NULL;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AOpen
- pascal TAppleEventPlayer * TCoreApplication::CreateServerPlayer(void)
- {
- return NULL;
- }
-
- // ============================== TCoreDocument =================================
- #pragma segment AOpen
- pascal void TCoreDocument::ICoreDocument(char aHCellSize, char aVCellSize,
- char aBlackPlayer, char aWhitePlayer)
- {
- gBusyCursor->ForceBusy();
- this->IDocument();
- fHCellSize = aHCellSize;
- fVCellSize = aVCellSize;
-
- fBlackPlayer = NULL;
- fWhitePlayer = NULL;
-
- fTurn = kBlackTurn;
-
- fIdleFreq =2;
-
- FailNonObject(fBlackPlayer = CreatePlayer(aBlackPlayer, kBlack));
- FailNonObject(fWhitePlayer = CreatePlayer(aWhitePlayer, kWhite));
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AOpen
- pascal TPlayer * TCoreDocument::CreatePlayer(char /*PlayerType*/, CellState /*aColor*/)
- {
- return NULL;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AOpen
- pascal void TCoreDocument::CreateTableSpecifier(AEDesc & aTableSpec)
- {
- AEDesc dDocName, dNull;
- Str255 aTitle;
- GetTitle(aTitle);
- FailOSErr(AECreateDesc(typeChar, (char *)aTitle, aTitle.Length(), dDocName));
- FailOSErr(AECreateDesc(typeNull, NULL, 0, dNull));
- FailOSErr(CreateObjSpecifier(cTable, dNull, formName, dDocName, TRUE, aTableSpec));
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AOpen
- pascal void TCoreDocument::DoMakeViews(Boolean /*forPrinting*/)
- {
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AOpen
- pascal void TCoreDocument::SetPlayerStatus(void)
- {
- if(IsObject(fBlackPlayer) && IsObject(fWhitePlayer))
- {
- PlayerStatus BlackStatus = fBlackPlayer->GetStatus();
- PlayerStatus WhiteStatus = fWhitePlayer->GetStatus();
-
- if((BlackStatus == kReady) && (WhiteStatus == kReady))
- {
- TSendTurnCommand * aSendTurnCommand = new TSendTurnCommand;
- aSendTurnCommand->ISendTurnCommand(this);
- PostCommand(aSendTurnCommand);
- }
- else if((BlackStatus == kNotReady) || (WhiteStatus == kNotReady))
- {
- }
- else if((BlackStatus == kFailed) || (WhiteStatus == kFailed))
- {
- TCloseDocumentCommand * aCloseCommand = new TCloseDocumentCommand;
- FailNIL(aCloseCommand);
- aCloseCommand->ICloseDocumentCommand(this);
- gTaroApplication->PostCommand(aCloseCommand);
- }
- }
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AClose
- pascal void TCoreDocument::Free()
- {
- FreeIfObject(fBlackPlayer);
- FreeIfObject(fWhitePlayer);
- inherited::Free();
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal TPlayer * TCoreDocument::GetPlayer(GameState aState)
- {
- if(aState == kWhiteTurn)
- return fWhitePlayer;
- else if(aState == kBlackTurn)
- return fBlackPlayer;
- else
- return NULL;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal TPlayer * TCoreDocument::GetTemporaryPlayer(void)
- {
- return GetPlayer(GetTurn());
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal TPlayer * TCoreDocument::GetAnotherPlayer(void)
- {
- GameState aState = GetTurn();
- if(aState == kWhiteTurn)
- return fBlackPlayer;
- else if(aState == kBlackTurn)
- return fWhitePlayer;
- else
- return NULL;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal GameState TCoreDocument::GetTurn(void)
- {
- if(fTurn == kBlackTurnPass)
- return kBlackTurn;
- else if(fTurn == kWhiteTurnPass)
- return kWhiteTurn;
- else
- return fTurn;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal CellState TCoreDocument::GetTurnCellState(void)
- {
- switch (fTurn)
- {
- case kBlackTurn:
- case kBlackTurnPass:
- return kBlack;
- case kWhiteTurn:
- case kWhiteTurnPass:
- return kWhite;
- default:
- return kEmpty;
- }
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TCoreDocument::SwitchTurn(Boolean NotPass)
- {
- if(NotPass)
- {
- if((fTurn == kBlackTurn) || (fTurn == kBlackTurnPass))
- fTurn = kWhiteTurn;
- else if((fTurn == kWhiteTurn) || (fTurn == kWhiteTurnPass))
- fTurn = kBlackTurn;
- }
- else
- {
- if((fTurn == kBlackTurnPass) || (fTurn == kWhiteTurnPass))
- GameOver();
- else if(fTurn == kBlackTurn)
- fTurn = kWhiteTurnPass;
- else if(fTurn == kWhiteTurn)
- fTurn = kBlackTurnPass;
- }
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TCoreDocument::SendTurn(void)
- {
- TPlayer * aPlayer;
-
- if(GetTurn() != kGameOver)
- {
- if(IsObject(aPlayer = GetTemporaryPlayer()))
- aPlayer->MyTurn();
- }
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TCoreDocument::PlayerMove(TRecordCell * aPlayerMove)
- {
- TPlayer * aPlayer;
-
- if(IsObject(aPlayer = GetAnotherPlayer()))
- aPlayer->YourMove(aPlayerMove);
- if(IsObject(aPlayerMove))
- SwitchTurn(TRUE);
- else
- SwitchTurn(FALSE);
-
- TSendTurnCommand * aSendTurnCommand = new TSendTurnCommand;
- aSendTurnCommand->ISendTurnCommand(this);
- PostCommand(aSendTurnCommand);
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TCoreDocument::GetCellSize(char & aHCellSize, char & aVCellSize)
- {
- aHCellSize = fHCellSize;
- aVCellSize = fVCellSize;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AClose
- pascal void TCoreDocument::GameClose(void)
- {
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AClose
- pascal void TCoreDocument::GameOver(void)
- {
- fTurn = kGameOver;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AClose
- pascal void TCoreDocument::Close(void)
- {
- GameClose();
- inherited::Close();
- }
-
-
- // ============================== TScene =================================
- #pragma segment AOpen
- pascal void TScene::IScene(TCoreDocument * aCoreDocument)
- {
- TList *aList;
- char column, row, aHCellSize, aVCellSize;
- CellPtr aCell;
-
- fCoreDocument = aCoreDocument;
- fCellList = NULL; // in case IDocument fails
- aList = NewList();
- fCellList = aList;
- fCellList->SetEltType("TCell"); // for Inspector window
-
- aList = NewList();
- fNeighborCellList = aList;
- fNeighborCellList->SetEltType("TCell"); // for Inspector window
-
- fCoreDocument->GetCellSize(aHCellSize, aVCellSize);
- for(row = 0; row < aVCellSize; row++)
- {
- for(column = 0; column < aHCellSize; column++)
- {
- FailSpaceIsLow();
- aCell = this->CreateCell(column, row);
- }
- }
-
- fBlack = fWhite = 0;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AOpen
- pascal TCell * TScene::CreateCell(char aColumn, char aRow)
- {
- TCell * aCell = new TCell;
- FailNIL(aCell);
- aCell->ICell(this, aColumn, aRow);
- fCellList->InsertLast(aCell);
- return aCell;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TScene::AddCellLast(TCell *aCell)
- {
- fCellList->InsertLast(aCell);
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal TCell * TScene::GetCell(char aColumn, char aRow)
- {
- char aHCellSize, aVCellSize;
-
- fCoreDocument->GetCellSize(aHCellSize, aVCellSize);
- return (TCell *)fCellList->At(aColumn + aRow * aHCellSize + 1);
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal CellState TScene::GetTurn(void)
- {
- return fTurn;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TScene::SetTurn(CellState aCellState)
- {
- fTurn = aCellState;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AClose
- pascal void TScene::GameOver(void)
- {
- fCoreDocument->GameOver();
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AClose
- pascal void TScene::Free()
- {
- if (IsObject(fCellList))
- fCellList->FreeList(); // frees Cell objects and List object
- FreeIfObject(fNeighborCellList);
- inherited::Free();
- }
-
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TScene::AddNeighborCellLast(TCell *aCell)
- {
- fNeighborCellList->InsertLast(aCell);
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ARes
- pascal TList * TScene::GetNeighborCellList(void)
- {
- return fNeighborCellList;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ARes
- pascal void TScene::ChangeCount(CellState aColor, short aNumber)
- {
- if(aColor == kWhite)
- fWhite += aNumber;
- else if(aColor == kBlack)
- fBlack += aNumber;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ARes
- pascal short TScene::GetCount(CellState aColor)
- {
- if(aColor == kWhite)
- return fWhite;
- else if(aColor == kBlack)
- return fBlack;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ARes
- pascal TCoreDocument * TScene::ReturnCoreDocument(void)
- {
- return fCoreDocument;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ARes
- pascal void TScene::SearchAvailableCell(void)
- {
- CObjectIterator iter(fNeighborCellList);
-
- fAvailable = 0;
- for (TCell * aCell = (TCell *)iter.FirstObject(); iter.More(); aCell = (TCell *)iter.NextObject())
- if(aCell->IsNeighbor())
- aCell->ScanAllDriection();
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ARes
- pascal TCell * TScene::SearchNeighborCellList(TRecordCell *theCoordinate)
- {
- CObjectIterator iter(fNeighborCellList);
-
- for (TCell * aCell = (TCell *)iter.FirstObject(); iter.More(); aCell = (TCell *)iter.NextObject())
- if(aCell->IsEqual(theCoordinate))
- return aCell;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ARes
- pascal void TScene::DeleteNeighborCell(TCell * aCell)
- {
- fNeighborCellList->Delete(aCell);
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ARes
- pascal short TScene::HasAvailableCell(void)
- {
- SearchAvailableCell();
- return fAvailable;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ARes
- pascal short TScene::GetAvailableCell(void)
- {
- return fAvailable;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ARes
- pascal void TScene::AddAvailable(void)
- {
- fAvailable++;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ARes
- pascal void TScene::ResetAvailable(void)
- {
- fAvailable = 0;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AOpen
- pascal void TScene::SetInitialState(void)
- {
- char aHCellSize, aVCellSize;
-
- ReturnCoreDocument()->GetCellSize(aHCellSize, aVCellSize);
- SetTurn(kWhite);
- GetCell(aHCellSize / 2 - 1, aVCellSize / 2) ->PutPiece();
- GetCell(aHCellSize / 2, aVCellSize / 2 - 1) ->PutPiece();
- SetTurn(kBlack);
- GetCell(aHCellSize / 2, aVCellSize / 2) ->PutPiece();
- GetCell(aHCellSize / 2 - 1, aVCellSize / 2 - 1) ->PutPiece();
- }
-
-
- // ============================== TCoordinate =================================
- #pragma segment ASelCommand
- pascal void TCoordinate::ICoordinate(const char aColumn, const char aRow)
- {
- fColumn = aColumn;
- fRow = aRow;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ARes
- pascal char TCoordinate::GetRow(void)
- {
- return fRow;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ARes
- pascal char TCoordinate::GetColumn(void)
- {
- return fColumn;
- }
-
- #pragma segment ARes
- // ------------------------------------------------------------------------
- pascal Boolean TCoordinate::IsEqual(TCoordinate *theCoordinate)
- {
- if((fRow == theCoordinate->GetRow()) &&
- (fColumn == theCoordinate->GetColumn()))
- return TRUE;
- else
- return FALSE;
- }
-
-
-
- // ============================== TRecordCell =================================
- #pragma segment ASelCommand
- pascal void TRecordCell::IRecordCell(const char aColumn, const char aRow, CellState aState)
- {
- ICoordinate(aColumn, aRow);
- fState = aState;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ARes
- pascal CellState TRecordCell::GetState(void)
- {
- return fState;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ARes
- pascal GameState TRecordCell::GetGameState(void)
- {
- if(fState == kBlack)
- return kBlackTurn;
- else if(fState == kWhite)
- return kWhiteTurn;
- else
- return kGameOver;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ARes
- pascal void TRecordCell::SetState(CellState aState)
- {
- fState = aState;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ARes
- pascal Boolean TRecordCell::IsEmpty(void)
- {
- if((fState == kEmpty) || (fState == kNeighbor) || (fState == kAvailable))
- return TRUE;
- else
- return FALSE;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ARes
- pascal Boolean TRecordCell::IsNeighbor(void)
- {
- if((fState == kNeighbor) || (fState == kAvailable))
- return TRUE;
- else
- return FALSE;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ARes
- pascal Boolean TRecordCell::IsAvailable(void)
- {
- if(fState == kAvailable)
- return TRUE;
- else
- return FALSE;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ARes
- pascal void TRecordCell::SetNeighbor(void)
- {
- if((fState == kEmpty) || (fState == kAvailable))
- fState = kNeighbor;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ARes
- pascal void TRecordCell::SetAvailable(void)
- {
- if((fState == kEmpty) || (fState == kNeighbor))
- fState = kAvailable;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ARes
- pascal TRecordCell * TRecordCell::CloneRecordCell(void)
- {
- TRecordCell * aRecordCell = new TRecordCell;
- FailNIL(aRecordCell);
- aRecordCell->IRecordCell(GetColumn(), GetRow(), fState);
- return aRecordCell;
- }
-
-
-
- // ============================== TCell =================================
- #pragma segment ASelCommand
- pascal void TCell::ICell(TScene * aScene, const char aColumn, const char aRow)
- {
- IRecordCell(aColumn, aRow, kEmpty);
- fScene = aScene;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ARes
- pascal TScene * TCell::GetScene(void)
- {
- return fScene;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ARes
- pascal void TCell::PutPiece(void)
- {
- Direction aDirection;
-
- SetState(kAvailable);
- for(aDirection = kNorth; aDirection <= kNorthWest; aDirection++)
- ScanOneDriection(aDirection, kSame, this);
- ChangeState();
- GetScene()->DeleteNeighborCell(this);
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ADoCommand
- pascal void TCell::SetNeighbor(void)
- {
- if(IsEmpty() && !IsNeighbor())
- GetScene()->AddNeighborCellLast(this);
- inherited::SetNeighbor();
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ARes
-
- TestState TestCellState(CellState State1, CellState State2)
- {
- if(((State1 == kWhite) && (State2 == kBlack)) ||
- ((State1 == kBlack) && (State2 == kWhite)))
- return kAnother;
- else if(((State1 == kWhite) && (State2 == kWhite)) ||
- ((State1 == kBlack) && (State2 == kBlack)))
- return kSame;
- else
- return kNoHit;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ARes
- pascal void TCell::ChangeState(void)
- {
- CellState aTurn;
-
- aTurn = fScene->GetTurn();
- if(IsEmpty())
- {
- SetNeighborCell();
- fScene->ChangeCount(aTurn, 1);
- }
- else if(TestCellState(GetState(), aTurn) == kAnother)
- {
- fScene->ChangeCount(aTurn, 1);
- fScene->ChangeCount(GetState(), -1);
- }
- SetState(aTurn);
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ARes
- pascal void TCell::GetCornerCoordinate(char & h, char & v,
- char & aHCellSize, char & aVCellSize)
- {
- char aColumn, aRow;
-
- (fScene->ReturnCoreDocument())->GetCellSize(aHCellSize, aVCellSize);
- aColumn = GetColumn();
- aRow = GetRow();
- h = (char)Min(aColumn, aHCellSize - aColumn - 1);
- v = (char)Min(aRow, aVCellSize - aRow - 1);
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ADoCommand
- pascal TCell * TCell::GetNeighborCell(Direction aDirection)
- {
- char aColumn, aRow, aHCellSize, aVCellSize;
-
- (fScene->ReturnCoreDocument())->GetCellSize(aHCellSize, aVCellSize);
- aColumn = GetColumn();
- switch(aDirection)
- {
- case kNorthEast:
- case kEast:
- case kSouthEast:
- if(++aColumn > aHCellSize - 1)
- return NULL;
- break;
- case kNorth:
- case kSouth:
- break;
- case kSouthWest:
- case kWest:
- case kNorthWest:
- if(--aColumn < 0)
- return NULL;
- break;
- }
- aRow = GetRow();
- switch(aDirection)
- {
- case kSouthEast:
- case kSouth:
- case kSouthWest:
- if(++aRow > aVCellSize - 1)
- return NULL;
- break;
- case kEast:
- case kWest:
- break;
- case kNorthWest:
- case kNorth:
- case kNorthEast:
- if(--aRow < 0)
- return NULL;
- break;
- }
- return fScene->GetCell(aColumn, aRow);
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ADoCommand
- pascal void TCell::ScanAllDriection(void)
- {
- Direction aDirection;
- Boolean anAvailable;
-
- anAvailable = FALSE;
- SetNeighbor();
- if(IsEmpty())
- for(aDirection = kNorth; aDirection <= kNorthWest; aDirection++)
- if(ScanOneDriection(aDirection, kSame, this) == kHit)
- anAvailable = TRUE;
- if(anAvailable)
- {
- SetAvailable();
- GetScene()->AddAvailable();
- }
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ADoCommand
- pascal void TCell::SetNeighborCell(void)
- {
- Direction aDirection;
- TCell * aCell;
-
- for(aDirection = kNorth; aDirection <= kNorthWest; aDirection++)
- if((aCell = GetNeighborCell(aDirection)) != NULL)
- aCell->SetNeighbor();
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ADoCommand
- pascal void TCell::HitCell(TCell * aTurnCell)
- {
- if(IsAvailable())
- aTurnCell->ChangeState();
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ADoCommand
- pascal TestState TCell::ScanOneDriection( Direction aDirection,
- TestState aTestState,
- TCell * anOriginalCell)
- {
- TCell * NextCell;
- CellState NextState, aTurn;
- TestState Condition;
-
- aTurn = GetScene()->GetTurn();
- if((NextCell = GetNeighborCell(aDirection)) == NULL)
- return kNoHit;
- if(NextCell->IsEmpty())
- return kNoHit;
- NextState = NextCell->GetState();
- Condition = TestCellState(aTurn, NextState);
- if(Condition == kSame)
- {
- if(aTestState == kAnother)
- return kHit;
- else
- return kNoHit;
- }
- if(Condition == kAnother)
- {
- if(NextCell->ScanOneDriection(aDirection, kAnother, anOriginalCell) == kHit)
- {
- anOriginalCell->HitCell(NextCell);
- return kHit;
- }
- else
- return kNoHit;
- }
- else
- return kNoHit;
- }
-
-
- // ============================== TPlayer =================================
- #pragma segment AOpen
- pascal void TPlayer::Initialize(void)
- {
- inherited::Initialize();
- fCoreDocument = NULL;
- fPermission = FALSE;
- fStatus = kNotReady;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AOpen
- pascal void TPlayer::IPlayer(TCoreDocument * aCoreDocument)
- {
- IObject();
- fCoreDocument = aCoreDocument;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TPlayer::MyTurn(void)
- {
- fPermission = TRUE;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TPlayer::MyMove(TRecordCell * aRecordCell)
- {
- if(fPermission)
- {
- fPermission = FALSE;
- fCoreDocument->PlayerMove(aRecordCell);
- }
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TPlayer::YourMove(TRecordCell * /*aRecordCell*/)
- {
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal TCoreDocument * TPlayer::GetCoreDocument(void)
- {
- return fCoreDocument;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AOpen
- pascal void TPlayer::SetCoreDocument(TCoreDocument * aCoreDocument)
- {
- fCoreDocument = aCoreDocument;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal Boolean TPlayer::GetPermission(void)
- {
- return fPermission;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal PlayerStatus TPlayer::GetStatus(void)
- {
- return fStatus;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TPlayer::SetStatus(PlayerStatus aStatus)
- {
- fStatus = aStatus;
- TStartDocumentCommand * aStartCommand = new TStartDocumentCommand;
- FailNIL(aStartCommand);
- aStartCommand->IStartDocumentCommand(fCoreDocument);
- gTaroApplication->PostCommand(aStartCommand);
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TPlayer::MouseMove(TRecordCell * /*aMouseMove*/)
- {
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AOpen
- pascal void TPlayer::GetName(Str255 &/*aString*/)
- {
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AClose
- pascal void TPlayer::GameOver(GameResult /*aResult*/)
- {
- }
-
-
- // ============================== TAppleEventPlayer =================================
- #pragma segment AOpen
- pascal void TAppleEventPlayer::Initialize(void)
- {
- inherited::Initialize();
- fTargetTransactionID = 0;
- fTarget = NULL;
- fMove = NULL;
- fWait = FALSE;
- AEDesc dNULL;
- FailOSErr(AECreateDesc(typeNull, NULL, 0, dNULL));
- fObjSpec = dNULL;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AOpen
- pascal void TAppleEventPlayer::IAppleEventPlayer(TCoreDocument * aCoreDocument, CellState aColor)
- {
- IPlayer(aCoreDocument);
- fMyColor = aColor;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AOpen
- pascal unsigned long TAppleEventPlayer::GetTargetTransactionID(void)
- {
- return fTargetTransactionID;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AOpen
- pascal void TAppleEventPlayer::SetTargetTransactionID(unsigned long aTargetTransactionID)
- {
- fTargetTransactionID = aTargetTransactionID;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AOpen
- pascal CellState TAppleEventPlayer::GetMyColor(void)
- {
- return fMyColor;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AOpen
- pascal void TAppleEventPlayer::SetMyColor(CellState aColor)
- {
- fMyColor = aColor;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AClose
- pascal void TAppleEventPlayer::GameOver(GameResult aResult)
- {
- inherited::GameOver(aResult);
- if(fTargetTransactionID != 0)
- {
- fTarget->SendEndTransaction(aResult, fTargetTransactionID, this);
- fTargetTransactionID = 0;
- }
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AClose
- pascal void TAppleEventPlayer::EndTransaction(void)
- {
- fTargetTransactionID = 0;
- TCloseDocumentCommand * aCloseCommand = new TCloseDocumentCommand;
- FailNIL(aCloseCommand);
- aCloseCommand->ICloseDocumentCommand(GetCoreDocument());
- gTaroApplication->PostCommand(aCloseCommand);
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TAppleEventPlayer::MyMove(TRecordCell * aMove)
- {
- if(GetPermission())
- inherited::MyMove(aMove);
- else
- {
- fMove = aMove;
- fWait = TRUE;
- }
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TAppleEventPlayer::MyTurn(void)
- {
- inherited::MyTurn();
- if(fWait)
- {
- TMoveCommand * aMoveCommand = new TMoveCommand;
- FailNIL(aMoveCommand);
- aMoveCommand->IMoveCommand(this, fMove);
- gTaroApplication->PostCommand(aMoveCommand);
-
- fMove = NULL;
- fWait = FALSE;
- }
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TAppleEventPlayer::YourMove(TRecordCell * aMove)
- {
- AEDesc dCellSpec;
-
- CreateTargetCellSpecifier(aMove, dCellSpec);
- fTarget->SendSetData(aMove, fTargetTransactionID, dCellSpec);
- FailOSErr(AEDisposeDesc(dCellSpec));
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal TTarget * TAppleEventPlayer::ReturnTarget(void)
- {
- return fTarget;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TAppleEventPlayer::SetTarget(TTarget * aTarget)
- {
- fTarget = aTarget;
- }
-
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TAppleEventPlayer::SetTargetTableSpecifier(AEDesc & aTargetSpec)
- {
- AEDesc anAEDesc;
- FailOSErr(AEDuplicateDesc(aTargetSpec, anAEDesc));
- fObjSpec = anAEDesc;
- }
-
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TAppleEventPlayer::Free(void)
- {
- AEDesc anAEDesc = fObjSpec;
- FailOSErr(AEDisposeDesc(anAEDesc));
- if(fWait)
- FreeIfObject(fMove);
- inherited::Free();
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ARes
- pascal void TAppleEventPlayer::CreateTargetCellSpecifier(TRecordCell * aCell, AEDesc & dCellSpec)
- {
- AEDesc dOffset;
- AEDesc dTableSpec;
- if(IsObject(aCell))
- {
- char aHSize, aVSize;
-
- GetCoreDocument()->GetCellSize(aHSize, aVSize);
- FailOSErr(CreateOffsetDescriptor(aCell->GetRow() * aHSize + aCell->GetColumn() + 1, dOffset));
- }
- else
- FailOSErr(CreateOffsetDescriptor(0, dOffset));
- AEDesc anAEDesc = fObjSpec;
- FailOSErr(AEDuplicateDesc(anAEDesc, dTableSpec));
- FailOSErr(CreateObjSpecifier(cCell, dTableSpec, formAbsolutePosition, dOffset, TRUE, dCellSpec));
- }
-
- // ============================== TTarget =================================
- #pragma segment AOpen
- pascal void TTarget::ITarget(AEAddressDesc & address)
- {
- IObject();
- fTargetAddress = address;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AOpen
- pascal void TTarget::CreateDesc(DescType typeCode, Ptr dataPtr, Size dataSize)
- {
- AEAddressDesc address;
-
- FailOSErr(AECreateDesc(typeCode, dataPtr, dataSize, address));
- ITarget(address);
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TTarget::Free(void)
- {
- AEDesc anAEDesc = fTargetAddress;
- FailOSErr(AEDisposeDesc(anAEDesc));
- inherited::Free();
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TTarget::SendSetData(TRecordCell * aMove, unsigned long aTransactionID, AEDesc & cellSpec)
- {
- TAppleEvent * theAppleEvent;
- long aData;
-
- theAppleEvent = CreateAppleEvent(kAECoreSuite, kAESetData, kSetDataReturnID, aTransactionID);
- theAppleEvent->WriteParameter(keyDirectObject, cellSpec);
- if(IsObject(aMove))
- aData = aMove->GetState();
- else
- aData = -1;
- theAppleEvent->WriteParameterPtr(keyAETheData, typeEnumerated, (Ptr)&aData, sizeof(aData));
- theAppleEvent->SetSendingMode(kAENoReply + kAECanInteract + kAECanSwitchLayer);
- theAppleEvent->Send();
- theAppleEvent->Free();
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AClose
- pascal void TTarget::SendEndTransaction(GameResult aResult, unsigned long theServer, TPlayer * /*theClient*/)
- {
- if(aResult == kClose)
- {
- TAppleEvent * theAppleEvent = CreateAppleEvent(kAECoreSuite, kAEEndTransaction, kEndTransactionReturnID, theServer);
- theAppleEvent->SetSendingMode(kAENoReply);
- theAppleEvent->Send();
- theAppleEvent->Free();
- }
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal TAppleEvent * TTarget::CreateAppleEvent(AEEventClass theAEEventClass,
- AEEventID theAEEventID,
- short returnID,
- long transactionID)
- {
- AppleEvent aMessage;
- AEAddressDesc anAEAddressDesc = fTargetAddress;
- FailOSErr(AECreateAppleEvent(theAEEventClass, theAEEventID, anAEAddressDesc, returnID, transactionID, aMessage));
- TAppleEvent * anAppleEvent = new TAppleEvent;
- anAppleEvent->InitializeFromMessage(aMessage, TRUE);
- return anAppleEvent;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TTarget::GetName(Str255 & aName)
- {
- aName = "";
- }
-
-
- // ============================== TServerTarget =================================
- #pragma segment AOpen
- pascal void TServerTarget::IServerTarget(AEAddressDesc & targetID)
- {
- ITarget(targetID);
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TServerTarget::SetName(const Str255 & userName)
- {
- fUserName = userName;
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TServerTarget::GetName(Str255 & aName)
- {
- aName = fUserName;
- }
-
-
- // ============================== TNoChangesCommand =================================
- #pragma segment AOpen
- pascal void TNoChangesCommand::INoChangesCommand(void)
- {
- ICommand(0, NULL, FALSE, FALSE, NULL);
- }
-
-
- // ============================== TCloseDocumentCommand =================================
- #pragma segment AOpen
- pascal void TCloseDocumentCommand::ICloseDocumentCommand(TCoreDocument * aDocument)
- {
- fDocument = aDocument;
- INoChangesCommand();
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TCloseDocumentCommand::DoIt(void)
- {
- fDocument->Close();
- fCommandDone = TRUE;
- }
-
-
- // ============================== TStartDocumentCommand =================================
- #pragma segment AOpen
- pascal void TStartDocumentCommand::IStartDocumentCommand(TCoreDocument * aDocument)
- {
- fDocument = aDocument;
- INoChangesCommand();
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TStartDocumentCommand::DoIt(void)
- {
- if(IsObject(fDocument))
- fDocument->SetPlayerStatus();
- fCommandDone = TRUE;
- }
-
-
- // ============================== TNewElementCommand =================================
- #pragma segment AOpen
- pascal void TNewElementCommand::INewElementCommand(AppleEvent & theAppleEvent, AppleEvent & Reply)
- {
- DescType typeCode;
- Size actualSize;
- OSType classType;
-
- InitializeFromAppleEvent(cNewElement, NULL, FALSE, FALSE, NULL, theAppleEvent, Reply);
- fMessage->ReadParameterPtr(keyAEObjectClass, typeType, typeCode, (Ptr)&classType, sizeof(classType), actualSize);
-
- if(classType == cTable)
- {
- short aHCellSize, aVCellSize;
- long aColor;
- AEDesc ObjSpec, targetSpec;
- AERecord ElementList;
-
- Str255 aName;
-
- fMessage->ReadParameter(keyAEInsertHere, typeWildCard, ObjSpec);
- fMessage->ReadParameter(keyAEData, typeAERecord, ElementList);
- FailOSErr(AEGetKeyPtr(ElementList, keyTaroColumnNumber, typeShortInteger, typeCode,
- (Ptr)&aHCellSize, sizeof(aHCellSize), actualSize));
- fServerHCellSize = RetranslateCellSize(aHCellSize);
- FailOSErr(AEGetKeyPtr(ElementList, keyTaroRowNumber, typeShortInteger, typeCode,
- (Ptr)&aVCellSize, sizeof(aVCellSize), actualSize));
- fServerVCellSize = RetranslateCellSize(aVCellSize);
- FailOSErr(AEGetKeyPtr(ElementList, keyTaroColor, typeEnumerated, typeCode,
- (Ptr)&aColor, sizeof(aColor), actualSize));
- fServerColor = (char)aColor;
- FailOSErr(AEGetKeyPtr(ElementList, keyTaroUserName, typeChar, typeCode,
- NULL, 0, actualSize));
- Handle ClientName = NewHandle(actualSize);
- FailNIL(ClientName);
- LockHandleHigh(ClientName);
- FailOSErr(AEGetKeyPtr(ElementList, keyTaroUserName, typeChar, typeCode,
- (Ptr)(*ClientName), actualSize, actualSize));
- HUnlock(ClientName);
- GetIText(ClientName, aName);
- fNewServer = (TAppleEventPlayer *)fMessage->GetTransactionID();
- DisposHandle(ClientName);
- ((TServerTarget *)(fNewServer->ReturnTarget()))->SetName(aName);
- FailOSErr(AEGetKeyDesc(ElementList, keyTaroTableSpecifier, typeWildCard, targetSpec));
- fNewServer->SetTargetTableSpecifier(targetSpec);
- FailOSErr(AEDisposeDesc(ElementList));
- FailOSErr(AEDisposeDesc(targetSpec));
- }
- else
- ReportError(errAEFail, 0);
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TNewElementCommand::DoIt(void)
- {
- FailInfo fi;
- if (fi.Try())
- {
- AEDesc aTargetObject;
-
- gTaroApplication->OpenNew(cNewServer);
- TCoreDocument * aCoreDocument = fNewServer->GetCoreDocument();
- if(IsObject(aCoreDocument))
- aCoreDocument->CreateTableSpecifier(aTargetObject);
- else
- {
- FailOSErr(AECreateDesc(typeNull, NULL, 0, aTargetObject));
- ReportError(errAEUserCanceled, 0);
- }
- HandleReply(aTargetObject);
- fi.Success();
- }
- else
- {
- AEDesc dNull;
- FailOSErr(AECreateDesc(typeNull, NULL, 0, dNull));
- ReportError(errAEFail, 0);
- HandleReply(dNull);
- fi.ReSignal();
- }
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TNewElementCommand::HandleReply(AEDesc & aTargetObject)
- {
- gTaroApplication->RemoveNewElementCommand();
- fReply->WriteParameter(keyDirectObject, aTargetObject);
- fReply->SetTransactionID(fNewServer->GetTargetTransactionID());
- fCommandDone = TRUE;
- FailOSErr(AEDisposeDesc(aTargetObject));
- }
-
- // ------------------------------------------------------------------------
- #pragma segment AOpen
- pascal void TNewElementCommand::GetServerParameter(char & aHCellSize, char & aVCellSize, char & aColor,
- TAppleEventPlayer * (& aServer))
- {
- aHCellSize = fServerHCellSize;
- aVCellSize = fServerVCellSize;
- aColor = fServerColor;
- aServer = fNewServer;
- }
-
-
- // ============================== TBeginTransactionCommand =================================
- #pragma segment AOpen
- pascal void TBeginTransactionCommand::IBeginTransactionCommand(AppleEvent & theAppleEvent, AppleEvent & Reply)
- {
- InitializeFromAppleEvent(0, NULL, FALSE, FALSE, NULL, theAppleEvent, Reply);
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ADoCommand
- pascal void TBeginTransactionCommand::DoIt(void)
- {
- FailInfo fi;
-
- if(fi.Try())
- {
- AEAddressDesc address;
- AppleEvent aReply = fReply->fMessage;
- FailOSErr(AEGetAttributeDesc(aReply, keyAddressAttr, typeWildCard, address));
- TServerTarget * aServerTarget = new TServerTarget;
- aServerTarget->IServerTarget(address);
- TAppleEventPlayer * aPlayer = gTaroApplication->CreateServerPlayer();
- aPlayer->Initialize();
- aPlayer->SetTarget(aServerTarget);
- aPlayer->SetTargetTransactionID(fMessage->GetTransactionID());
- fReply->WriteLong(keyTransactionIDAttr, (long)aPlayer);
- fi.Success();
- }
- else
- {
- fReply->WriteLong(keyTransactionIDAttr, 0);
- ReportError(errAEFail, 0);
- fi.ReSignal();
- }
- fCommandDone = TRUE;
- }
-
- // ============================== TSetDataCommand =================================
- #pragma segment AOpen
- pascal void TSetDataCommand::ISetDataCommand(AppleEvent & theAppleEvent, AppleEvent & Reply)
- {
- InitializeFromAppleEvent(0, NULL, FALSE, FALSE, NULL, theAppleEvent, Reply);
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TSetDataCommand::DoIt(void)
- {
- FailInfo fi;
- long longError = errAEFail;
-
- if (fi.Try())
- {
- TAppleEventPlayer * aPlayer = (TAppleEventPlayer *)fMessage->GetTransactionID();
- FailNonObject(aPlayer);
- if(IsMemberClassID(aPlayer, GetClassIDFromName("TAppleEventPlayer")))
- {
- AEDesc ObjSpec, cellToken;
- DescType actualType;
- Size actualSize;
- long theData;
- TRecordCell * aMove = NULL;
-
- fMessage->ReadParameterPtr(keyAETheData, typeEnumerated, actualType,
- (Ptr)&theData, sizeof(theData), actualSize);
- if(theData == -1)
- {
- aPlayer->MyMove(aMove);
- longError = noErr;
- }
- else
- {
- fMessage->ReadParameter(keyDirectObject, typeObjectSpecifier, ObjSpec);
- FailOSErr(AEResolve(ObjSpec, kAEIDoMinimum, cellToken));
- FailOSErr(AEDisposeDesc(ObjSpec));
- if(cellToken.descriptorType == typeTObjectPtr)
- {
- aMove = (TRecordCell *)(**((TObjectPtrHandle)cellToken.dataHandle));
- FailOSErr(AEDisposeToken(cellToken));
- FailNonObject(aMove);
- if(IsMemberClassID(aMove, GetClassIDFromName("TRecordCell")))
- {
- aMove->SetState((CellState)theData);
- aPlayer->MyMove(aMove);
- longError = noErr;
- }
- else
- longError = errAENoSuchObject;
- }
- else
- longError = errAENoSuchObject;
- }
- }
- else
- longError = errAEEventNotHandled;
- fi.Success();
- }
- else
- {
- ReportError(errAEFail,0);
- fi.ReSignal();
- }
- if(longError != noErr)
- {
- ReportError(errAEFail,0);
- }
- fCommandDone = TRUE;
- }
-
- // ============================== TMoveCommand =================================
- #pragma segment AOpen
- pascal void TMoveCommand::IMoveCommand(TPlayer * aPlayer, TRecordCell * aMove)
- {
- fPlayer = aPlayer;
- fMove = aMove;
- ICommand(0, aPlayer->GetCoreDocument(), FALSE, FALSE, NULL);
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TMoveCommand::DoIt(void)
- {
- fPlayer->MyMove(fMove);
- fCommandDone = TRUE;
- }
-
- // ============================== TSendTurnCommand =================================
- #pragma segment AOpen
- pascal void TSendTurnCommand::ISendTurnCommand(TCoreDocument * aCoreDocument)
- {
- ICommand(0, aCoreDocument, FALSE, FALSE, NULL);
- }
-
- // ------------------------------------------------------------------------
- #pragma segment ASelCommand
- pascal void TSendTurnCommand::DoIt(void)
- {
- ((TCoreDocument *)fContext)->SendTurn();
- fCommandDone = TRUE;
- }
-